#coding=utf-8
import wx
from wx import grid
from com.jumang.content.GetContent import GetContent
from com.jumang.config.Config import myConfig
ID_TIMER=10005
class SimpleTable(grid.PyGridTableBase):
  header=['name','type','state','last time','message','server','cron']
    
  def setFrame(self,parent):
    self.mainFrame=parent
    
  def GetNumberRows(self):
    #print len(getContent.servers)
    #print getContent.servers
    return len(self.mainFrame.servers)
  def GetNumberCols(self):
    return len(self.header)
  
  def GetValue(self,row,col):
    #print 'get Value'
    if col==0:
      return self.mainFrame.servers[row].getName()
    elif col==1:
      return self.mainFrame.servers[row].type
    elif col==2:
      return str(self.mainFrame.servers[row].errorNo)
    elif col==3:
      return str(self.mainFrame.servers[row].lastTime)
    elif col==4:
      return self.mainFrame.servers[row].errorInfo
    elif col==5:
      return self.mainFrame.servers[row].server
    elif col==6:
      return self.mainFrame.servers[row].cronRule.ruleString
    else:
      return ''
  def GetColLabelValue(self,col):
    return self.header[col]
  
  def GetRowLabelValue(self,row):
    return str(row+1)
  
    

class StateDialog(wx.Dialog):
  originWidth=125
  originHeight=30
  originPos=(270,3) 
  
  def __init__(self,parent):
    wx.Dialog.__init__(self,None,wx.ID_ANY,'Servers State',\
    size=(730,480),style=wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX)
    #self.panel = wx.Panel(self, wx.ID_ANY)
    self.parentWin=parent
    self.topSizer        = wx.BoxSizer(wx.VERTICAL)
    #contentSizer        = wx.BoxSizer(wx.VERTICAL)
    self.dataGrid=grid.Grid(self,wx.ID_ANY)
    self.dataTable=SimpleTable()
    self.dataTable.setFrame(parent)
    self.dataGrid.SetTable(self.dataTable)
    self.dataGrid.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
    
    
    self.topSizer.Add(self.dataGrid, 1, wx.EXPAND)
    okButton= wx.Button(self,wx.ID_ANY,'OK')
    self.topSizer.Add(okButton, 0, wx.EXPAND)
    self.dataGrid.AutoSize()
    wx.EVT_CLOSE(self, self.onOKButton)
    self.Bind(wx.EVT_BUTTON, self.onOKButton, okButton)
    
    self.SetSizer(self.topSizer)
    self.timer=wx.Timer(self,ID_TIMER)
    self.timer.Start(myConfig.refreshSpeed)
    self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, self.onRightClick,self.dataGrid)
    wx.EVT_TIMER(self,ID_TIMER,self.onTime)
    
    
  def onOKButton(self,e):
    self.Destroy()
    return True
    
  def onTime(self,e):
    self.dataGrid.ForceRefresh()

  def onRightClick(self,e):
    self.dataGrid.SelectRow(e.GetRow())
    menu = wx.Menu()
    checkItem=menu.Append(-1,'Check server') 
    self.Bind(wx.EVT_MENU, self.onCheck, checkItem)
    self.PopupMenu(menu, e.GetPosition())
    menu.Destroy()
 
  def onCheck(self,e):
    rows=self.dataGrid.GetSelectedRows()
    self.parentWin.servers[rows[0]].forceCheck()
#    for server in self.servers:
#      try:
#        server.forceCheck()
#      except:
#        pass
#      self.control.ChangeValue(datetime.now().strftime('%m.%d %H:%M:%S')+' '+self.information.getChar())
  
  
  